package cn.qayy.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.qayy.constants.Constant;
import cn.qayy.dao.SynchronizationMapper;
import cn.qayy.service.SynchronizationService;
@SuppressWarnings("all")
@Service("synchronizationImpl")
public class SynchronizationImpl implements SynchronizationService {
	
	@Resource
	public SynchronizationMapper synchronizationMapper;
 
	@Override
	public void basicUpdateRegister(Set<String> provinces, Set<String> citys, Set<String> countrys) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("provinces", provinces);
		params.put("citys", citys);
		params.put("countrys", countrys);
		synchronizationMapper.basicUpdateRegister(params);
	}

	@Override
	public void basicUpdatePlans(Set<String> provinces, Set<String> citys, Set<String> countrys) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("provinces", provinces);
		params.put("citys", citys);
		params.put("countrys", countrys);
		synchronizationMapper.basicUpdatePlans_product(params);
		synchronizationMapper.basicUpdatePlans_tx(params);
		synchronizationMapper.basicUpdatePlans_qudao(params);
		
		if(provinces != null && provinces.contains("北京")) {
			//特殊情况更新 T1A 
			synchronizationMapper.basicUpdatePlans_other(params);
		}
	}

	@Override
	public void registerUpdatePlans(Set<String> cnames) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("cnames", cnames);
		synchronizationMapper.registerUpdatePlans1(params);
		synchronizationMapper.registerUpdatePlans2(params);
	}

	@Override
	public void plansUpdateRegister(Set<String> cnames) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("cnames", cnames);
		synchronizationMapper.plansUpdateRegister(params);
	}

	@Override
	public void updateXsCount(Set<String> cnames) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("cnames", cnames);
		synchronizationMapper.updateXsCount_A12_Rx(params);
		synchronizationMapper.updateXsCount_A24_Rx(params);
		synchronizationMapper.updateXsCount_A36_Rx(params);
		synchronizationMapper.updateXsCount_A48_Rx(params);
		synchronizationMapper.updateXsCount_A20_Rx(params);
		synchronizationMapper.updateXsCount_A30_Rx(params);
		synchronizationMapper.updateXsCount_A40_Rx(params);
		synchronizationMapper.updateXsCount_A240_Rx(params);
		synchronizationMapper.updateXsCount_A480_Rx(params);
		
		synchronizationMapper.updateXsCount_Y24_Rx(params);
		synchronizationMapper.updateXsCount_Y36_Rx(params);
		synchronizationMapper.updateXsCount_Y48_Rx(params);
		synchronizationMapper.updateXsCount_Y20_Rx(params);
		synchronizationMapper.updateXsCount_Y30_Rx(params);
		synchronizationMapper.updateXsCount_Y40_Rx(params);
		synchronizationMapper.updateXsCount_Y60_Rx(params);
		synchronizationMapper.updateXsCount_Z48_Rx(params);
		synchronizationMapper.updateXsCount_Z24_Rx(params);
		synchronizationMapper.updateXsCount_Z36_Rx(params);
		synchronizationMapper.updateXsCount_Z20_Rx(params);
		synchronizationMapper.updateXsCount_Z30_Rx(params);
		synchronizationMapper.updateXsCount_Z40_Rx(params);
		synchronizationMapper.updateXsCount_Z60_Rx(params);
		
		synchronizationMapper.updateXsCount_A12_OTC(params);
		synchronizationMapper.updateXsCount_A24_OTC(params);
		synchronizationMapper.updateXsCount_A36_OTC(params);
		synchronizationMapper.updateXsCount_A48_OTC(params);
		synchronizationMapper.updateXsCount_A20_OTC(params);
		synchronizationMapper.updateXsCount_A30_OTC(params);
		synchronizationMapper.updateXsCount_A40_OTC(params);
		synchronizationMapper.updateXsCount_A240_OTC(params);
		synchronizationMapper.updateXsCount_A480_OTC(params);
		
		synchronizationMapper.updateXsCount_Y24_OTC(params);
		synchronizationMapper.updateXsCount_Y36_OTC(params);
		synchronizationMapper.updateXsCount_Y48_OTC(params);
		synchronizationMapper.updateXsCount_Y20_OTC(params);
		synchronizationMapper.updateXsCount_Y30_OTC(params);
		synchronizationMapper.updateXsCount_Y40_OTC(params);
		synchronizationMapper.updateXsCount_Y60_OTC(params);
		
		synchronizationMapper.updateXsCount_Z48_OTC(params);
		synchronizationMapper.updateXsCount_Z24_OTC(params);
		synchronizationMapper.updateXsCount_Z36_OTC(params);
		synchronizationMapper.updateXsCount_Z20_OTC(params);
		synchronizationMapper.updateXsCount_Z30_OTC(params);
		synchronizationMapper.updateXsCount_Z40_OTC(params);
		synchronizationMapper.updateXsCount_Z60_OTC(params);
		
		//非新疆
		synchronizationMapper.updateXsCount_B5_other(params);
		synchronizationMapper.updateXsCount_B10_other(params);
		
		//新疆
		synchronizationMapper.updateXsCount_B5_xj_zy(params);
		synchronizationMapper.updateXsCount_B10_xj_zy(params);
		synchronizationMapper.updateXsCount_B5_xj_xb(params);
		synchronizationMapper.updateXsCount_B10_xj_xb(params);
		synchronizationMapper.updateXsCount_B5_xj_yf(params);
		synchronizationMapper.updateXsCount_B10_xj_yf(params);
		
	}

	@Override
	public List<String> selectCnamesByPCC(Set<String> provinces, Set<String> citys, Set<String> countrys) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("provinces", provinces);
		params.put("citys", citys);
		params.put("countrys", countrys);
		return synchronizationMapper.selectCnamesByPCC(params);
	}
	/**
	 * 1、2、3 顺序不能乱
	 */
	@Override
	public void syncUpdatePlansIgnoreXsCount(Set<String> provinces, Set<String> citys, Set<String> countrys) {
		if(provinces!=null && !provinces.isEmpty() && citys!=null && !citys.isEmpty() && countrys!=null && !countrys.isEmpty()) {
			/***1、基础库同步更新注册库**/
			basicUpdateRegister(provinces,citys,countrys);
			//通过省份、地市、区县查询对应的入货乙方们 
			Set<String> cnames = new HashSet<String>(Constant.SMALL_CAPACITY);
			//去掉重复的cnames
			cnames.addAll(selectCnamesByPCC(provinces,citys,countrys));
			/***2、注册库同步更新流向 **/
			registerUpdatePlans(cnames);
			/***3、基础库同步更新流向**/
			basicUpdatePlans(provinces,citys,countrys);
		}
		
	}
	/**
	 * 1、2、3、4顺序不能乱
	 */
	@Override
	public void syncUpdatePlansAndUpdateXsCount(Set<String> provinces, Set<String> citys, Set<String> countrys) {
		/***1、基础库同步更新注册库**/
		basicUpdateRegister(provinces,citys,countrys);
		//通过省份、地市、区县查询对应的入货乙方们 
		Set<String> cnames = new HashSet<String>(Constant.SMALL_CAPACITY);
		//去掉重复的cnames
		cnames.addAll(selectCnamesByPCC(provinces,citys,countrys));
		/***2、注册库同步更新流向 **/
		registerUpdatePlans(cnames);
		/***3、更新销售折量 **/
		updateXsCount(cnames);
		/***4、基础库同步更新流向**/
		basicUpdatePlans(provinces,citys,countrys);
	}

	@Override
	public void syncUpdateOther(Set<String> provinces, Set<String> citys, Set<String> countrys) {
		Map<String, Object> params = new HashMap<String,Object>(Constant.INIT_CAPACITY);
		params.put("provinces", provinces);
		params.put("citys", citys);
		params.put("countrys", countrys);
		if(provinces != null && provinces.contains("北京")) {
			//特殊情况更新 T1A 
			synchronizationMapper.basicUpdatePlans_other(params);
		}
	}
}
